<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Database extends Common
{
    protected $db = '', $datadir =  './public/Data/';
    function _initialize(){
        parent::_initialize();
        $db=db('');
        $this->db = Db::connect();
    }
	//数据库备份
	public function index(){
		 header("content-type:text/html;charset=utf-8");   
		 $db = Db::connect()->query("SHOW TABLE STATUS");

		$this->assign('db',$db);
		return $this->fetch();
	}

	//单条记录优化表
	public function optimize(){
    	$tables = input('param.tables');
		$list = Db::query("OPTIMIZE TABLE `{$tables}`");
		if($list){	
          admin_log("数据表'{$tables}'优化完成！");
          $this->success("数据表'{$tables}'优化完成！",'admin/database/index','',1); 
		}else{
			$this->error("数据表'{$tables}'优化出错请重试！");
		}
		
	}
    
	//批量优化表
	public function optimizes(){
		if(request() -> isAjax()){
           $tables=$_POST['tables'];
           $tables = implode('`,`', $tables);
           $list = Db::query("OPTIMIZE TABLE `{$tables}`");
			if($list){	
	          admin_log("数据表优化完成！");
	          return 2;
			}else{
				return 3;
			}
		}else{
              return 1;
	    }
	}

	//单条记录修复表
	public function repair(){
		$tables = input('param.tables');
		$list = Db::query("REPAIR TABLE `{$tables}`");
		if($list){	
          admin_log("数据表'{$tables}'修复完成完成！");
          $this->success("数据表'{$tables}'修复完成！",'admin/database/index','',1); 
		}else{
			$this->error("数据表'{$tables}'优化出错请重试！");
		}
	}

	//批量修复表
	public function repairs(){	
	    if(request() -> isAjax()){
	           $tables=$_POST['tables'];
	           $tables = implode('`,`', $tables);
	           $list = Db::query("REPAIR TABLE `{$tables}`");
				if($list){	
		          admin_log("数据表修复完成！");
		          return 4;
				}else{
					return 5;
				}
			}else{
	              return 1;
		    }
	}
   
}